package tedu.study.ooday04_2;

public class Test {
    public static void main(String[] args) {
        //Dog d = (Dog)new Animal("1",2,"3");
        //public int a = 5;

        //1）用多态，向上造型将统一超类的派生类装入一个数组，方便遍历
        //Animal o = new Animal();
        Animal[] animals = new Animal[5];
        animals[0] = new Dog("小黑",1,"黑");
        animals[1] = new Dog("小灰",2,"灰");
        animals[2] = new Chick("小花",1,"黄");
        animals[3] = new Fish("小金",1,"金");
        animals[4] = new Fish("小银",1,"银");
        for(int i=0;i<animals.length;i++){
            System.out.println(animals[i].name);
            animals[i].eat();
            animals[i].drink();

            Animal a = animals[i];
            if(a instanceof Dog){
                Dog dog = (Dog)a;
                dog.lookHome();
            }
            if(a instanceof Chick){
                Chick chick = (Chick)a;
                chick.layEggs();
            }
            if(a instanceof Swim){
                Swim s = (Swim)a;
                //((Swim) a).swim();
                s.swim();
            }
        }

        //2）用向上造型优化方法
        /*Master master = new Master();
        master.feed(animals[0]);
        master.feed(animals[2]);
        master.feed(animals[3]);*/

        //匿名内部类
        /*Animal a = new Animal("1",2,"3") {
            @Override
            void eat() {
                System.out.println("现在吃");
            }
        };
        System.out.println(a.name);
        System.out.println(a.age);
        System.out.println(a.color);
        a.drink();
        a.eat();*/


        //null指针异常
        /*Dog dog = new Dog("小黑",2,"黑");
        System.out.println(dog.name);
        dog = null;
        //System.out.println(dog.name);*/


        //三个派生类数组的建立和访问
        /*Dog[] dogs = new Dog[2];
        dogs[0] = new Dog("小黑",1,"黑");
        dogs[1] = new Dog("小灰",2,"灰");
        for(int i=0;i<dogs.length;i++){
            System.out.println(dogs[i].name);
            dogs[i].drink();
        }
        Chick[] chicks = new Chick[1];
        chicks[0] = new Chick("小花",1,"黄");
        for(int i=0;i<chicks.length;i++){
            System.out.println(chicks[i].age);
            chicks[i].eat();
        }
        Fish[] fish = new Fish[2];
        fish[0] = new Fish("小金",1,"金");
        fish[1] = new Fish("小银",1,"银");
        for(int i=0;i<fish.length;i++){
            System.out.println(fish[i].color);
            fish[i].swim();
        }*/
    }
}
